﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractForeEnd;
using HamcoWcs.DAL;
using HamcoWcs.Models.Common;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.ForeEndModels.UITasks;
using HamcoWcs.Service.Common;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace HamcoWcs.Service.ForeEnd
{
    [AutoInject(typeof(IForeEndTaskService), InjectType.Scope)]
    public class ForeEndTaskService : ForeEndBaseService, IForeEndTaskService
    {
        public ForeEndTaskService(WcsDbContext wcsDbContext, IMapper mapper, ILoggerService logger) : base(wcsDbContext, mapper, logger)
        {
        }

        public ForeEndResult QueryHistoryMainTask(QueryMainTaskHistoryModel query)
        {


            //var queryable = _wcsDbContext.MainTaskBaks
            //                             .AsQueryable()
            //                             .AsNoTracking()
            //                             .Include(d => d.SubTasks)
            //                             .WhereIf(x => x.SubTasks.Any(s => s.ToLoc.Contains(query.RoutePath)), !string.IsNullOrEmpty(query.RoutePath))
            //                             .OrderByDescending(d => d.CreatedTime)
            //                             .VagueWhereOut(query, out int count, out int pageCount)
            //                             .ToList();

            var queryable = _wcsDbContext.MainTaskBaks
                  .AsQueryable()
                  .AsNoTracking()
                  .OrderByDescending(d => d.CreatedTime)
                  .WhereIf(x => x.Path.Contains(query.RoutePath), !string.IsNullOrEmpty(query.RoutePath))
                  .VagueWhereOut(query, out int count, out int pageCount)
                  
                  .ToList();
            //var a = _wcsDbContext.MainTaskBaks
            //    .AsQueryable()
            //    .AsNoTracking()
            //    .WhereIf(x => x.Path.Contains(query.RoutePath), !string.IsNullOrEmpty(query.RoutePath))
            //    .VagueWhereOut(query, out int count, out int pageCount)
            //    .OrderByDescending(d => d.CreatedTime)
            //    .ToList();
            var dataDtos = _mapper.Map<List<UIMainTask>>(queryable);
            //foreach (var data in dataDtos)
            //{
            //    data.SubTasks = data.SubTasks.OrderBy(d => d.TaskSeq).ToList();
            //}
            return new ForeEndResult(dataDtos, count, pageCount);
        }

        public ForeEndResult QuerySubTaskById(BaseModel model)
        {
            var queryable = _wcsDbContext.MainTaskBaks
                .AsNoTracking()
                .Where(x => x.Id == model.Id)
                .Include(x => x.SubTasks)
                .ToList();

            var task = queryable.FirstOrDefault();
            if (task != null)
            {
                var subtasks = _mapper.Map<List<UISubTask>>(task.SubTasks);
                return new ForeEndResult(subtasks);
            }
            else
            {
                return new ForeEndResult(new List<UISubTask>());
            }

        }

        public ForeEndResult QueryMainTask(QueryMainTask query)
        {
            var queryable = _wcsDbContext.MainTasks
                                         .AsQueryable()
                                         .AsNoTracking()
                                         .OrderByDescending(d => d.CreatedTime)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .Include(d => d.SubTasks)
                                         .ToList();

            var dataDtos = _mapper.Map<List<UIMainTask>>(queryable);

            foreach (var data in dataDtos)
            {
                data.SubTasks = data.SubTasks.OrderBy(d => d.TaskSeq).ToList();
            }

            return new ForeEndResult(dataDtos, count, pageCount);
        }



        public ForeEndResult QuerySubTask(QuerySubTask query)
        {
            var queryable = _wcsDbContext.SubTasks
                                         .AsQueryable()
                                         .AsNoTracking()
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .OrderByDescending(d => d.CreatedTime)
                                         .ToList();

            var dataDtos = _mapper.Map<List<UISubTask>>(queryable);
            return new ForeEndResult(dataDtos, count, pageCount);
        }

    }
}
